<?php

class Util {
	
	public static function fileCache($name, $value = '', $expire = 3600) {
		$filename = md5 ( $name );
		
		// 获取值
		if (! $value) {
			$cacheValue = file_get_contents ( APP_PATH . '/runtime/cache/' . $filename );
			
			if (! $cacheValue || time () > ( int ) substr ( $cacheValue, 0, 10 )) {
				return false;
			}
			
			return unserialize ( substr ( $cacheValue, 10 ) );
		}
		
		// 赋值
		return ( bool ) file_put_contents ( APP_PATH . '/runtime/cache/' . $filename, (time () + $expire) . serialize ( $value ) );
	}
	
	public static function ClearLastStr($str) {
		if (strlen ( $str ) == 0)
			return "";
		else
			return substr ( $str, 0, strlen ( $str ) - 1 );
	}
	
	public static function htmlpage($offset, $allnums, $url, $offset_str) {
		
		// 总页数
		if (floor ( $allnums / MAX_ROWS ) < ($allnums / MAX_ROWS)) {
			$allPageNums = floor ( $allnums / MAX_ROWS ) + 1;
		} else {
			$allPageNums = floor ( $allnums / MAX_ROWS );
		}
		
		// 当前偏移
		//$offset = floor(($page-1)/MAX_ROWS)*MAX_ROWS;
		

		// 本页的最大页数
		$maxPageOfCurrent = $offset + MAX_ROWS > $allPageNums ? $allPageNums : $offset + MAX_ROWS;
		
		$out = "";
		// 上一页
		if ($offset > 0) {
			$out .= "<a href=\"" . $url . "\"><img src='" . APP_URL . "/images/start.gif' width='11' height='9' border='0' align='absmiddle'>&nbsp;Start</a>&nbsp;&nbsp;";
			$out .= "<a href=\"" . $url . "/" . $offset_str . "/" . ($offset - MAX_ROWS) . "\"><img src='" . APP_URL . "/images/previous.gif' width='6' height='9' alt='Previous'  border='0' align='absmiddle'>&nbsp;Previous</a>&nbsp;&nbsp;";
		} else {
			$out .= "<img src='" . APP_URL . "/images/start_off.gif' width='11' height='9' border='0' align='absmiddle'>&nbsp;Start&nbsp;&nbsp;";
			$out .= "<img src='" . APP_URL . "/images/previous_off.gif' width='6' height='9' alt='Previous'  border='0' align='absmiddle'>&nbsp;Previous&nbsp;&nbsp;";
		}
		
		// 中间
		$out .= "<span class='pageNumbers'>(" . ($offset + 1) . " -" . (($offset + MAX_ROWS) > $allnums ? $allnums : ($offset + MAX_ROWS)) . " of " . $allnums . ")</span>&nbsp;&nbsp;";
		
		// 打印下一页
		if ($allnums > ($offset + MAX_ROWS)) {
			$out .= "<a href=\"" . $url . "/" . $offset_str . "/" . ($offset + MAX_ROWS) . "\">Next&nbsp;<img src='" . APP_URL . "/images/next.gif' width='6' height='9'  border='0' align='absmiddle'></a>&nbsp;&nbsp;<a href=\"" . $url . "/" . $offset_str . "/" . (($allPageNums - 1) * MAX_ROWS) . "\">End&nbsp;<img src='" . APP_URL . "/images/end.gif' width='11' height='9' border='0' align='absmiddle'></a>";
		} else {
			$out .= "Next&nbsp;<img src='" . APP_URL . "/images/next_off.gif' width='6' height='9'  border='0' align='absmiddle'>&nbsp;&nbsp;End&nbsp;<img src='" . APP_URL . "/images/end_off.gif' width='11' height='9' border='0' align='absmiddle'>";
		}
		
		// 跳页
		//$out .= "&nbsp;&nbsp;&nbsp;<input type=\"text\" style=\"font-size:9px;height:15px;width:30px;\" id=\"jump\"><input type=\"image\"  src=\"".APP_URL."/images/next.gif\" width='6' height='9'  border='0' align='absmiddle' id=\"jump_btn\" value='".$url."'>";
		

		return $out;
	}
	
	public static function wappage($page, $allnums, $url, $max_rows) {
		
		// 总页数
		if (floor ( $allnums / $max_rows ) < ($allnums / $max_rows)) {
			$allPageNums = floor ( $allnums / $max_rows ) + 1;
		} else {
			$allPageNums = floor ( $allnums / $max_rows );
		}
		
		$out = "";
		
		// 上一页
		if ($page > 1) {
			$out .= "<a href=\"" . $url . "/page/" . ($page - 1) . "\">上一页</a>&nbsp;";
		}
		
		// 打印下一页
		if ($page < $allPageNums) {
			$out .= "<a href=\"" . $url . "/page/" . ($page + 1) . "\">下一页&nbsp;</a>";
		}
		
		$out .= "&nbsp;第" . $page . "页&nbsp;共" . $allPageNums . "页";
		
		return $out;
	}
	
	// 按页数
	public static function htmlpage2($page, $allnums, $url, $offset_str) {
		
		// 总页数
		if (floor ( $allnums / MAX_ROWS ) < ($allnums / MAX_ROWS)) {
			$allPageNums = floor ( $allnums / MAX_ROWS ) + 1;
		} else {
			$allPageNums = floor ( $allnums / MAX_ROWS );
		}
		
		// 当前偏移
		$offset = floor ( ($page - 1) / MAX_ROWS ) * MAX_ROWS;
		
		// 本页的最大页数
		$maxPageOfCurrent = $offset + MAX_ROWS > $allPageNums ? $allPageNums : $offset + MAX_ROWS;
		
		$out = "";
		// 上一页
		if ($page > 0) {
			$out .= "<a href=\"" . $url . "\"><img src='" . APP_URL . "/images/start.gif' width='11' height='9' border='0' align='absmiddle'>&nbsp;Start</a>&nbsp;&nbsp;";
			$out .= "<a href=\"" . $url . "/" . $offset_str . "/" . ($offset - MAX_ROWS) . "\"><img src='" . APP_URL . "/images/previous.gif' width='6' height='9' alt='Previous'  border='0' align='absmiddle'>&nbsp;Previous</a>&nbsp;&nbsp;";
		} else {
			$out .= "<img src='" . APP_URL . "/images/start_off.gif' width='11' height='9' border='0' align='absmiddle'>&nbsp;Start&nbsp;&nbsp;";
			$out .= "<img src='" . APP_URL . "/images/previous_off.gif' width='6' height='9' alt='Previous'  border='0' align='absmiddle'>&nbsp;Previous&nbsp;&nbsp;";
		}
		
		// 中间
		$out .= "<span class='pageNumbers'>(" . ($offset + 1) . " -" . (($offset + MAX_ROWS) > $allnums ? $allnums : ($offset + MAX_ROWS)) . " of " . $allnums . ")</span>&nbsp;&nbsp;";
		
		// 打印下一页
		if ($allnums > ($offset + MAX_ROWS)) {
			$out .= "<a href=\"" . $url . "/" . $offset_str . "/" . ($offset + MAX_ROWS) . "\">Next&nbsp;<img src='" . APP_URL . "/images/next.gif' width='6' height='9'  border='0' align='absmiddle'></a>&nbsp;&nbsp;<a href=\"" . $url . "/" . $offset_str . "/" . (($allPageNums - 1) * MAX_ROWS) . "\">End&nbsp;<img src='" . APP_URL . "/images/end.gif' width='11' height='9' border='0' align='absmiddle'></a>";
		} else {
			$out .= "Next&nbsp;<img src='" . APP_URL . "/images/next_off.gif' width='6' height='9'  border='0' align='absmiddle'>&nbsp;&nbsp;End&nbsp;<img src='" . APP_URL . "/images/end_off.gif' width='11' height='9' border='0' align='absmiddle'>";
		}
		
		// 跳页
		//$out .= "&nbsp;&nbsp;&nbsp;<input type=\"text\" style=\"font-size:9px;height:15px;width:30px;\" id=\"jump\"><input type=\"image\"  src=\"".APP_URL."/images/next.gif\" width='6' height='9'  border='0' align='absmiddle' id=\"jump_btn\" value='".$url."'>";
		

		return $out;
	}
}